﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Microsoft.SharePoint;
using SPThumbnailExtender.Interne;
using System.Drawing;

namespace SPThumbnailExtender.Handler
{
    internal static class HandlerHelper
    {
        public static void ProcessRequest(HttpContext context, string strDoc, int? imgWidth, int? imgHeight, int? iPage, bool? bCount, bool? bCache, Type ThumbnailType)
        {

            if (string.IsNullOrEmpty(strDoc))
                return;

            Uri uriDoc;
            if (strDoc.StartsWith("http"))
                uriDoc = new Uri(strDoc, UriKind.Absolute);
            else
                Uri.TryCreate(new Uri(SPContext.Current.Site.Url, UriKind.Absolute), strDoc, out uriDoc);

            ThumbnailBase thBase = ThumbnailFactory.GenerateThumbnail(uriDoc);
            if (ThumbnailType != null && thBase.GetType() != ThumbnailType)
                return;

            if (bCache.HasValue && bCache.Value && bCount.HasValue && bCount.Value)
            {
                context.Response.Clear();
                context.Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
                context.Response.ContentType = "text/plain";

                int iCount = 0;
                RevertToAppPool.UseAppPoolIdentify(delegate()
                {
                    iCount = thBase.GetCountThumbnailCache();
                });
                context.Response.Write(iCount);
                context.Response.Flush();
                return;
            }
            else if (bCount.HasValue && bCount.Value)
            {
                context.Response.Clear();
                context.Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
                context.Response.ContentType = "text/plain";
                int iCount = 0;
                RevertToAppPool.UseAppPoolIdentify(delegate()
                {
                    iCount = thBase.GetCount();
                });
                context.Response.Write(iCount);

                context.Response.Flush();
                return;
            }
            bool IsDocumentExist = false;
            RevertToAppPool.UseAppPoolIdentify(delegate()
            {
                IsDocumentExist = Helper.IsDocumentExistInDatabase(uriDoc);
            });
            if (bCache.HasValue && bCache.Value && IsDocumentExist)
            {
                Bitmap bmp = null;
                RevertToAppPool.UseAppPoolIdentify(delegate()
                {
                    if (iPage.HasValue)
                        Helper.GetThumbnail(uriDoc, iPage.Value, out bmp);
                    else
                        Helper.GetThumbnail(uriDoc, 0, out bmp);
                });
                if (bmp == null)
                    return;
                
                context.Response.Clear();
                context.Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
                context.Response.ContentType = "image/png";
                if (imgHeight.HasValue && imgWidth.HasValue)
                {
                    using (Image img = Helper.FixedSize(bmp, imgWidth.Value, imgHeight.Value))
                    {
                        img.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Png);
                    }

                    bmp.Dispose();
                    return;
                }
                bmp.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Png);
                context.Response.Flush();
                bmp.Dispose();
                return;
            }
            else
            {
                RevertToAppPool.UseAppPoolIdentify(delegate()
                {
                    Helper.CreateWorkItem(uriDoc);
                }); 

                using (Bitmap bmp = new Bitmap(typeof(MobileGenericHandler).Assembly.GetManifestResourceStream("SPThumbnailExtender.thumbext100.png")))
                {
                    if (imgHeight.HasValue && imgWidth.HasValue)
                    {
                        context.Response.Clear();
                        context.Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
                        context.Response.ContentType = "image/png";

                        using (Image img = Helper.FixedSize(bmp, imgWidth.Value, imgHeight.Value))
                        {
                            img.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Png);
                        }

                        context.Response.Flush();
                        return;
                    }
                    bmp.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Png);
                    context.Response.Flush();
                }
            }

        }
    }
}

